
///////////////////////////////////////////////////////////////////////////////
// Simple "Hello World" widget that demonstrates the WRTKit UI toolkit.

// Reference to the WRTKit user interface manager and main view.
var uiManager;
var mainView;
var dom="http://www.evernote.com";
// Reference to controls in the main view.
var helloButton;
var camButton;
var fileButton;
var nameField;

//about view label control
var aboutLabel;

// Constants for menu item identifiers.
var MENU_ITEM_ABOUT = 0;

// Called from the onload event handler to initialize the widget.
function init() {
	
    // set tab-navigation mode and show softkeys
    // (only if we are in the WRT environment)
    if (window.widget) {
        widget.setNavigationEnabled(false);
        menu.showSoftkeys();
	//create about menu
	var aboutMenuItem = new MenuItem("About", MENU_ITEM_ABOUT);
	aboutMenuItem.onSelect = menuItemSelected;
	menu.append(aboutMenuItem);

    }
    
    // create UI manager
    uiManager = new UIManager();

    // create main view
    mainView = new ListView(null, "Evernote S60");
    
	// create Evernote Mobile view
	var content = "hello";

	try{ 
//TODO REMOVE BEFORE DEPLOYING
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); }

catch(e){ }
 
	jQuery.get("http://www.evernote.com/mobile/Home.action",  
	function(data){ 
		content=$("#fullPage",data).html();
		content = content.replace(/href="/gi, 'href="'+dom);
		content = content.replace(/src="/gi, 'src="'+dom);
		everView = new ContentPanel(null, null, content, true, false);
		mainView.addControl(everView);
	});
	
    
	var nb = new NavigationButton(null,null,"nav button");
	mainView.addControl(nb);
	//Create about view
	aboutView = new ListView(null, "About: Evernote S60");
	
    // add a text field to the view
    nameField = new TextField(null, "Enter your name");
    mainView.addControl(nameField);
    
    // add a button to the view
    helloButton = new FormButton(null, "Say Hello!");
    helloButton.addEventListener("ActionPerformed", helloButtonClicked);
    mainView.addControl(helloButton);
	
	camButton = new FormButton(null, "Take Picture!");
    camButton.addEventListener("ActionPerformed", camButtonClicked);
    mainView.addControl(camButton);
    
	fileButton = new FormButton(null, "Send a file to Evernote!");
    fileButton.addEventListener("ActionPerformed", fileButtonClicked);
    mainView.addControl(fileButton);

	// About lable control
	aboutLabel = new Label();
	aboutView.addControl(aboutLabel);
	
    // display the main view
    //uiManager.setView(mainView);
	showMainView();
	
	//Test for camera 
//	isCameraAvailable();
}

// Show main view.
function showMainView() {
    
    // set right softkey to "exit"
    if (window.widget) {
        menu.setRightSoftkeyLabel("", null);
    // show the main view
	 window.widget.prepareForTransition("fade");
    uiManager.setView(mainView);
	window.widget.performTransition("fade");
	
	} else{
		uiManager.setView(mainView);
	}
    
    
}
// Callback for when menu items are selected.
function menuItemSelected(id) {
    switch (id) {
       	case MENU_ITEM_ABOUT:
			showAboutView();
			break;
    }
}

//Displays the About view
function showAboutView(){
	aboutLabel.setText("Welcome to my widget. This Widget includes software licensed from Nokia &copy 2008");
	
	setAboutViewSoftkeys();
	 window.widget.prepareForTransition("fade");
	uiManager.setView(aboutView);
	window.widget.performTransition("fade");
}

// Sets the soft keys for about view.
function setAboutViewSoftkeys() {
    if (window.widget) {
        // set right soft key to "Ok" (returns to main view)
        menu.setRightSoftkeyLabel("Ok", showMainView);
    }
}

// Called when the hello-button is clicked.
function helloButtonClicked(event) {
    var name = nameField.getText();
    if (name.length == "") {
        uiManager.showNotification(3000, "warning", "Please enter your name!");
    } else {
        uiManager.showNotification(3000, "info", "Hello " + name + "!");
    }
}

function camButtonClicked(event) {
    uiManager.showNotification(2000, "info", "Opening Camera...");
    //widget.openApplication(0x101f857a, "");
var so = device.getServiceObject("Service.AppManager", "IAppManager");
		console.info("setup: so: %s", so);
		var result = so.IAppManager.LaunchApp({ApplicationID:"s60uid://0x101F857A", Options:{Mode:"Chained"}});
}
function fileButtonClicked(event) {
    uiManager.showNotification(2000, "info", "Please select a file...");
    if (device != "undefined") {
		var so = device.getServiceObject("Service.MediaManagement", "IDataSource");
		console.info("setup: so: %s", so);
		
		  // Setup input params using dot syntax
  var criteria = new Object();
  criteria.Type = 'FileInfo';
  criteria.Filter = new Object();
  criteria.Filter.FileType = 'Sound';
  criteria.Sort = new Object();
  criteria.Sort.Key = 'FileSize';

  // set the image id for callback function
  imgid_callback1 = imgId;

  try {
    // Media Management supports asynchronous call
    var result = so.IDataSource.GetList(criteria, gal_callback);
    if (!checkError("IDataSource::getListAsync", result, DIV_ID, imgId)) {
		showIMG(imgId, "");
	}
  }
  catch (e) {
    showIMG(imgId,"no");
    alert ("getListAsync: " + e);
  }
	}
}

function gal_callback(transId, eventCode, result)
{
  console.info("callback: transId: %d  eventCode: %d result.ErrorCode: %d", transId, eventCode, result.ErrorCode);
  if(!checkError("IDataSource::getListAsync",result,DIV_ID,imgid_callback1)) {
    document.getElementById(DIV_ID).innerHTML = showIterableList(result.ReturnValue);
  }
}


// Feature Information
function isCameraAvailable()
{
   // Initialize the criteria for the service object and obtain the
    // information
    var criteria = new Object();
    criteria.Entity = "Features";
    criteria.Key = "CAMERA";
    try {
         var systemServiceObj = device.getServiceObject("Service.SysInfo", "ISysInfo");
		var result = systemServiceObj.ISysInfo.GetInfo(criteria);
    }
    catch (ex) 
    {
        alert(ex);
        return;
    }
 
    if(result.ReturnValue.Status)
	console.log("Camera is available");
    else
	console.log("Camera is not available");
}